package cn.com.dhc.danlu.widget.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import java.math.BigDecimal;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CalculatePriceUtil;
import cn.com.dhc.danlu.util.CommonUtils;

/**
 * @author wuhaiyang
 */
public class OrderConfirmCalHolder {
    //原价
    private TextView tvGoodsPrice;
    //丹露红包抵扣
    private TextView tvEnvelopDeduction;
    // 促销
    private TextView tvPreferential;
    // 商品优惠券抵扣
    private TextView tvGoodsCouponDed;
    // 经销商红包抵扣
    private TextView tvDealerEnvelopDed;
    //零元订单提示
    private TextView tvZeroOrderTips;

    public OrderConfirmCalHolder(View view) {
        tvGoodsPrice = (TextView) view.findViewById(R.id.tv_goods_price);
        tvEnvelopDeduction = (TextView) view.findViewById(R.id.tv_envelop_deduction);
        tvPreferential = (TextView) view.findViewById(R.id.tv_preferential);
        tvGoodsCouponDed = (TextView) view.findViewById(R.id.tv_goods_coupon_deduction);
        tvDealerEnvelopDed = (TextView) view.findViewById(R.id.tv_dealer_envelop_deduction);
        tvZeroOrderTips = (TextView) view.findViewById(R.id.tv_zero_order_tips);
    }

    public void isZeroOrder(boolean isZero) {
        tvZeroOrderTips.setVisibility(isZero ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取所有折扣后的订单总价
     */
    public BigDecimal getAfterDiscountPrice() {
        BigDecimal res = getDanluEnvelopMaxDeduction();
        if (null != tvEnvelopDeduction.getTag()) {
            // 丹露红包抵扣
            res = CalculatePriceUtil.subtraction(res.toPlainString(), (String) tvEnvelopDeduction.getTag());
        }
        return res;
    }

    /**
     * 获取丹露红包最大抵扣金额
     */
    public BigDecimal getDanluEnvelopMaxDeduction() {
        BigDecimal bigDecimal = new BigDecimal(0);
        if (null != tvGoodsPrice.getTag()) {
            String originPrice = (String) tvGoodsPrice.getTag();
            bigDecimal = new BigDecimal(originPrice);
            // 减去促销优惠
            if (null != tvPreferential.getTag()) {
                bigDecimal = CalculatePriceUtil.subtraction(bigDecimal.toPlainString(), (String) tvPreferential.getTag());
            }
            // 减去商品优惠券
            if (null != tvGoodsCouponDed.getTag()) {
                bigDecimal = CalculatePriceUtil.subtraction(bigDecimal.toPlainString(), (String) tvGoodsCouponDed.getTag());
            }
            // 减去经销商红包
            if (null != tvDealerEnvelopDed.getTag()) {
                bigDecimal = CalculatePriceUtil.subtraction(bigDecimal.toPlainString(), (String) tvDealerEnvelopDed.getTag());
            }
        }
        return bigDecimal;
    }

    /**
     * 刷新底部所有经销商红包抵扣
     *
     * @param deduction
     *         单位:分
     */
    @SuppressLint("SetTextI18n")
    public void refreshDealerTotalEnvDed(String deduction) {
        tvDealerEnvelopDed.setTag(deduction);
        int compare = CalculatePriceUtil.compare(deduction, "0");
        View parent = (View) tvDealerEnvelopDed.getParent();
        if (compare == 0 || compare == -1) {
            parent.setVisibility(View.GONE);
        } else {
            parent.setVisibility(View.VISIBLE);
            tvDealerEnvelopDed.setText("- ¥ " + formateMoney(deduction));
        }
    }

    /**
     * 刷新所有优惠券抵扣总金额
     *
     * @param deduction
     *         分
     */
    @SuppressLint("SetTextI18n")
    public void refreshGoodsCouponDed(String deduction) {
        tvGoodsCouponDed.setTag(deduction);
        int compare = CalculatePriceUtil.compare(deduction, "0");
        View parent = (View) tvGoodsCouponDed.getParent();
        if (compare == 0 || compare == -1) {
            parent.setVisibility(View.GONE);
        } else {
            parent.setVisibility(View.VISIBLE);
            tvGoodsCouponDed.setText("- ¥ " + formateMoney(deduction));
        }
    }

    /**
     * 刷新商品促销
     *
     * @param price
     *         单位是分
     */
    @SuppressLint("SetTextI18n")
    public void refreshPreferential(String price) {
        tvPreferential.setTag(price);
        int compare = CalculatePriceUtil.compare(price, "0");
        View parent = (View) tvPreferential.getParent();
        if (compare == 0 || compare == -1) {
            parent.setVisibility(View.GONE);
        } else {
            parent.setVisibility(View.VISIBLE);
            tvPreferential.setText("- ¥ " + formateMoney(price));
        }
    }

    private void showOriginPrice() {
        View parent = (View) tvGoodsPrice.getParent();
        if (parent.getVisibility() != View.VISIBLE) {
            parent.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示商品原价 只要存在任一优惠 就显示
     */
    @Deprecated
    private void isShowOriginPrice() {
        View parent = (View) tvGoodsPrice.getParent();
        TextView[] textViews = new TextView[]{
                tvPreferential, tvGoodsCouponDed, tvDealerEnvelopDed, tvEnvelopDeduction
        };
        boolean isHiden = false;
        BigDecimal bdTotal = new BigDecimal(0);
        for (TextView tv : textViews) {
            if (null != tv.getTag()) {
                String amount = (String) tv.getTag();
                bdTotal = CalculatePriceUtil.addition(bdTotal.toPlainString(), amount);
                if (CalculatePriceUtil.compare(amount, "0") == 1) {
                    parent.setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
        int compare = CalculatePriceUtil.compare(bdTotal.toPlainString(), "0");
        if (compare == 0 || compare == -1) {
            parent.setVisibility(View.GONE);
        }
    }

    /**
     * 刷新丹露红包价格
     *
     * @param money
     *         分
     */
    @SuppressLint("SetTextI18n")
    public void refreshDanluEnvelopDeduction(String money) {
        tvEnvelopDeduction.setTag(money);
        int compare = CalculatePriceUtil.compare(money, "0");
        View parent =
                (View) tvEnvelopDeduction.getParent();
        if (compare == 0 || compare == -1) {
            parent.setVisibility(View.GONE);
        } else {
            parent.setVisibility(View.VISIBLE);
            tvEnvelopDeduction.setText("- ¥ " + formateMoney(money));
        }
    }


    /**
     * 刷新商品原价
     *
     * @param originTotalPrice
     *         分
     */
    @SuppressLint("SetTextI18n")
    public void refreshOriginPrice(String originTotalPrice) {
        tvGoodsPrice.setTag(originTotalPrice);
        tvGoodsPrice.setText(" ¥ " + formateMoney(originTotalPrice));
        showOriginPrice();
    }

    /**
     * 分转换成元 并且始终保留两位小数
     *
     * @param money
     *         单位：分
     */
    private String formateMoney(String money) {
        BigDecimal bigDecimal = CommonUtils.fenToYuan(new BigDecimal(money));
        return CommonUtils.formateRate(bigDecimal.toPlainString());
    }

    public Context getContext() {
        return tvGoodsPrice.getContext();
    }
}